package com.enqualcomm.kids.mvp.resetpassword;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.activities.AgreementActivity;
import com.enqualcomm.kids.activities.ChooseCountryActivity;
import com.enqualcomm.kids.activities.GetMessageActivity;
import com.enqualcomm.kids.config.Constants;
import com.enqualcomm.kids.view.BasePager;
import com.enqualcomm.kids.view.SlidingView;

import common.utils.PromptUtil;

public class ResetPwdStep1Pager extends BasePager implements OnClickListener {

    private EditText register_username_et;
    private View next_step_btn;
    private CheckBox agreement_cb;
    private boolean inputOK;
    private TextView choosed_contry_name_tv;
    private TextView choosed_contry_number_et;
    private SlidingView slidingView;
    private TextView select_counry;

    public ResetPwdStep1Pager(Context context) {
        super(context);
    }

    @Override
    public View initView() {
        View root = View.inflate(ct, R.layout.pager_register_step1, null);
        next_step_btn = root.findViewById(R.id.next_step_btn);
        next_step_btn.setOnClickListener(this);
        root.findViewById(R.id.agreement_tv).setOnClickListener(this);
        select_counry = (TextView) root.findViewById(R.id.select_counry);
        root.findViewById(R.id.choose_country_rl).setOnClickListener(this);
        choosed_contry_name_tv = (TextView) root.findViewById(R.id.choosed_contry_name_tv);
//        choosed_contry_name_tv.setText("中国大陆");
        choosed_contry_number_et = (TextView) root.findViewById(R.id.choosed_contry_number_et);
//        choosed_contry_number_et.setText("+86");

        register_username_et = (EditText) root.findViewById(R.id.register_username_et);
        register_username_et.requestFocus();
        register_username_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                //输入结束
                String inputNumber = s.toString().trim();
                if (inputNumber.matches(Constants.USERNAME_REGEX)) {
                    inputOK = true;
//					if (agreement_cb.isChecked()) {
//						next_step_btn.setEnabled(true);//选中checkbox并且符合手机号码的基本格式
//					}
                    if (slidingView.isAtEnd()) {
                        slidingView.resetSlidView();
                    }
                    slidingView.setSlideViewEnable(true);
                } else {
                    inputOK = false;
                    if (slidingView.isAtEnd()) {
                        slidingView.resetSlidView();
                    }
                    slidingView.setSlideViewEnable(false);
//					next_step_btn.setEnabled(false);
                }
            }
        });

        agreement_cb = (CheckBox) root.findViewById(R.id.agreement_cb);
        root.findViewById(R.id.agreement_ll).setVisibility(View.INVISIBLE);
        root.findViewById(R.id.description_tv).setVisibility(View.INVISIBLE);
        slidingView = (SlidingView) root.findViewById(R.id.sliding_view);
        slidingView.setOnScrollEndListener(new SlidingView.OnScrollEndListener() {
            @Override
            public void scrollEndSuccess() {
                if (inputOK) {
                    if (agreement_cb.isChecked()) {
                        next_step_btn.setEnabled(true);//选中checkbox并且符合手机号码的基本格式
                    }
                } else {
                    next_step_btn.setEnabled(false);
                }
            }

            @Override
            public void resetView() {
                next_step_btn.setEnabled(false);
            }
        });
        return root;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.next_step_btn:
                String username = register_username_et.getText().toString().trim();
                String country = choosed_contry_number_et.getText().toString().trim();

                if ("".equals(country)) {
                    slidingView.resetSlidView();
                    slidingView.setSlideViewEnable(false);
                    next_step_btn.setEnabled(false);
                    PromptUtil.toast(ct, ct.getString(R.string.select_contury));
                    return;
                }
                country = country.substring(1, country.length());

                next_step_btn.setEnabled(false);
                ((GetMessageActivity) ct).nextPage(1, username + ":" + country);
                break;
            case R.id.agreement_tv:
                //跳转到用户协议
                ct.startActivity(new Intent(ct, AgreementActivity.class));
                break;
            case R.id.choose_country_rl:
                //跳转到选择国家界面
                ((GetMessageActivity) ct).startActivityForResult(new Intent(ct, ChooseCountryActivity.class), 10000);
                break;
            default:
                break;
        }
    }

    @Override
    public void clear() {
        register_username_et.setText("");
    }


    public void setBtnEnable(boolean enable) {
        next_step_btn.setEnabled(enable);
    }


    public void onCountryChooded(String name, String countryCode) {
        if (TextUtils.isEmpty(name)) {
            name = ct.getString(R.string.regist_main_land);
        }
        select_counry.setText(name);
        if (countryCode.length() > 1) {
//            select_counry.setVisibility(View.INVISIBLE);
            choosed_contry_number_et.setText(countryCode);
            if (inputOK) {
                slidingView.resetSlidView();
                slidingView.setSlideViewEnable(true);
            }
        } else {
            choosed_contry_number_et.setText("");
        }
        register_username_et.requestFocus();
    }

    public void setUsername(String username) {
        register_username_et.setText(username);
        register_username_et.setSelection(username.length());
    }

    public void resetView() {
        slidingView.resetSlidView();
        slidingView.setSlideViewEnable(true);
    }
}
